import HttpUtils from '/pages/HttpUtils/HttpUtils';
App({
    ...HttpUtils.func,
    onLaunch: function (options) {
            let that = this;
   
            // 展示本地存储能力
            let logs = wx.getStorageSync('logs') || []
            logs.unshift(Date.now())
            wx.setStorageSync('logs', logs);
            // 登录  
            wx.qy.login({
                success: res => {
                    var newUrl = getApp().globalData.dangjianUrl + "/member/memberLogin";
                    var params = {};
                    params["authCode"] = res.code;
                    params["corpId"] = '';
                    params["timestamp"] = Date.parse(new Date()) / 1000;
                    params["nonce"] = Date.parse(new Date()) / 1000;
                    params["appKey"] = getApp().globalData.appKey;
                    params["sign"] = that.jsonSort(params) + "&appSecret=" + getApp().globalData.appSecret;
                    var data = that.getMd5(JSON.stringify(params));

                    if (res.code) {
                        //发起网络请求
                        wx.request({
                            url: newUrl,
                            data: {
                                "requestJson": data
                            },
                            success: (res) => {
                                if (res.data.result.code == '1') {
                                    getApp().globalData.userName = res.data.member.userName;
                                    getApp().globalData.userId = res.data.member.userId;
                                    getApp().globalData.iconUrl = res.data.member.iconUrl;
                                    getApp().globalData.partyId = res.data.member.partyId;
                                    getApp().globalData.partyName = res.data.member.partyName;
                                    getApp().globalData.corpId = res.data.member.corpId;
                                    getApp().globalData.corpName = res.data.member.corpName;
                                    getApp().globalData.goodsItemName = res.data.member.goodsItemName; //商品套餐名称(即商品规格名称)
                                    getApp().globalData.serviceStopTime = res.data.member.serviceStopTime; //服务到期时间
                                }

                                //头像获取
                                wx.qy.getAvatar({
                                    success: function (res) {
                                        var avatar = res.avatar
                                        getApp().globalData.iconUrl = avatar;
                                        console.log(1111, avatar);
                                    },
                                    fail: function (res) {
                                        console.log(res.fail_reason)
                                    }
                                })

                                if (getApp().globalData.userId == 0) {
                                    wx.reLaunch({
                                        url: "../moneyPop/moneyPop?userId=" + getApp().globalData.userId
                                    })
                                }
                                  if (that.userInfoReadyCallback) {
                                      console.log('call back');
                                      that.userInfoReadyCallback(res.data.member && res.data.member.userId || getApp().globalData.userId);
                                  }
                            }
                        })
                    } else {
                        console.log('登录失败！' + res.errMsg)
                    }
                }
            });
            // 获取用户信息
            wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                wx.getUserInfo({
                    success: res => {
                    // 可以将 res 发送给后台解码出 unionId
                    this.globalData.userInfo = res.userInfo;
                

                    // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                    // 所以此处加入 callback 以防止这种情况
                    if (this.userInfoReadyCallback) {
                        this.userInfoReadyCallback(res)
                    }
                    }
                })
                }
            }
            })
  },
  // 获取日期格式化
  formatToDate(startDate, endDate) {
    var date = startDate.substring(0, startDate.indexOf(':') - 2);
    var startTime = startDate.substring(startDate.indexOf(':') - 2, startDate.length);
    var endTime = endDate.substring(endDate.indexOf(':') - 2, endDate.length);
    var week = "周";
    switch (new Date(date).getDay()) {
      case 0:
        week += "日";
        break;
      case 1:
        week += "一";
        break;
      case 2:
        week += "二";
        break;
      case 3:
        week += "三";
        break;
      case 4:
        week += "四";
        break;
      case 5:
        week += "五";
        break;
      case 6:
        week += "六";
        break;
    }
    var fmt = 'MM月dd日'
    date = null == date || "undefined" == typeof (date) ? new Date() : date;
    if ("string" == typeof (date)) {
      date = date.replace(/\-/g, '/');
      date = new Date(date);
    }
    var o = {
      "M+": date.getMonth() + 1,                 //月份   
      "d+": date.getDate(),                    //日   
      "h+": date.getHours(),                   //小时   
      "m+": date.getMinutes(),                 //分   
      "s+": date.getSeconds(),                 //秒   
      "q+": Math.floor((date.getMonth() + 3) / 3),    ///季度   
      "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
      if (new RegExp("(" + k + ")").test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      }
    }
    return fmt + ' ' + week + ' ' + startTime + '-' + endTime;
  },
  globalData: {
    // 定义一些系统变量
    // dangjianUrl: "http://47.98.56.45:9001/public/api",   //测试
    // dangjianUrl: "http://wasu.vaiwan.com/public/api", 		//跳转测试 
    // dangjianUrl: "http://djportaltest.wasu.cn/public/api",
    // dangjianUrl: "http://127.0.0.1:9001/public/api", 		//本地 
    //  dangjianUrl: 'http://10.88.47.234:9001/public/api',
    dangjianUrl : 'https://cpvas.wasu.cn/public/api',
    // 站点ID
    siteCode: 8000,
    //组织id
    partyId: 0,
    //用户ID
     userId: 0,	     // 有数据	 
    // userId: 366,         //没数据
    //用户名称
    userName: "邓宏炎",
    iconUrl: "",
    //组织名称
    partyName: "",
    // corpId:"dingd51f4e7cc9e1a63535c2f4657eb6378f",
    corpId: "dingcebbf29dd319089c35c2f4657eb6378f",
    corpName: "",
    appKey: "apptest",
    appSecret: "qwer123456", //默认值 
    imageType: '320d',
    goodsItemName: "",  //商品套餐名称(即商品规格名称)
    serviceStopTime: "", //服务到期时间
  } 
})